#ifndef __KEY_RECOMMANDER_H__
#define __KEY_RECOMMANDER_H__

#include <string>
#include <queue>
#include <set>
#include <vector>

//#include "server/CandidateResult.h"

using std::string;
using std::vector;
using std::priority_queue;
using std::queue;
using std::set;

class MyResult{
public:
    MyResult(string word, int freq, int dist = 999)
    : _word(word), _freq(freq), _dist(dist){}
    
    string getWord() const{
        return _word;
    }

    int getFreq() const{
        return _freq;
    }

    int getDist() const{
        return _dist;
    }

    void setDist(int dist){
        _dist = dist;
    }

private:
    string _word;
    int _freq;
    int _dist;
};

struct MyCompare
{
    bool operator()(const MyResult &lhs, const MyResult &rhs){
        if (lhs.getDist() > rhs.getDist()) // 编辑距离小的排前面
        {
            return true;
        }
        else if (lhs.getDist() == rhs.getDist())
        {
            if (lhs.getFreq() < rhs.getFreq()) // 频率大的排前面
                return true;
            else if (lhs.getFreq() == rhs.getFreq())
            {
                if (lhs.getWord() > rhs.getWord()) // 字典序排序
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        else
            return false;
    }
};

class KeyRecommander{
public:
    KeyRecommander() = default;
    ~KeyRecommander() = default;
    string doQuery(const string&);

private:
    void execute();
    void queryIndexTable();
    void statistic(const string&, set<int> &, priority_queue<MyResult, vector<MyResult>, MyCompare> &resultQue);
    int distance(const string &, const string &rhs);

private:
    // string _queryWord;
    // queue<CandidateResult> _priqueue;
    // TcpConnectPtr _conn;
};

#endif